package com.dingding.study.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.study.entity.Permission;
import com.dingding.study.mapper.PermissionMapper;
import com.dingding.study.service.PermissionService;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    Set<String> perList = Sets.newHashSet(
//            "MEDBRAIN_RESEARCH",
//            "TEAM_MANAGEMENT",
            "HEALTH_MANAGEMENT",
            "PHYSICAL_MANAGEMENT",
            "WRITE",
            "CHECK",
            "RECHECK",
            "OPTIMIZATION",
            "OK/PE_CASES_OPERATION",
            "DATA_BOARD",
            "OK/PE_MEMBER_MANAGEMENT",
            "OK/PE_POINT_ACCOUNT",
            "OK/PE_HEALTH_PREISCRIPTION_MANAGEMENT",
            "OK/PE_CHRONIC_DISEASE_DATA",
            "OK/PE_QUESTIONARY_MANAGEMENT",
            "OK/PE_TEAM_MAINTENANCE",
            "OK/PE_REPORT_MANAGEMENT",
            "OK/PE_BELIEVE_USER_MANAGEMENT",
            "OK/PE_ORDER_MANAGEMENT",
            "OK/PE_SERVICE_STATISTICS",
            "write_FORWARD_DEFINITION",
            "OK/PE_CASE_MARK_CONFIRM",
            "OK/PE_CASES_OPERATION",
            "OK/PE_COMPOSE_DISEASE_POPULAR_MATERIAL",
            "OK/PE_COMPOSE_HEALTH_POPULAR_MATERIAL",
            "check_forward_definition",
            "xxx",
            "OK/PE_CONFIRM_DISEASE_POPULAR_MATERIAL",
            "OK/PE_AUDIT_HEALTH_POPULAR_MATERIAL",
            "OK/PE_OPTIMIZE_DISEASE_WESTERN_SPEC",
            "OK/PE_OPTIMIZE_DISEASE_TCM_SPEC",
            "OK/PE_OPTIMIZE_INFECTION_SPEC",
            "OK/PE_OPTIMIZE_SURGERY_SURVEILLIANCE_SPEC",
            "OK/PE_OPTIMIZE_ANESTHESIA",
            "recheck_forward_definition",
            "recheck_疾病科普",
            "OK/PE_TEAM_DASHBOARD",
            "OK/PE_TEAM_DATA_MANAGEMENT",
            "QUESTION_BROWSE",
            "CONFIGURE_QUESTION",
            "ADD_QUESTION",
            "EDIT_QUESTION",
            "FACTOR_MANAGEMENT",
            "DIMENSION_SETTING",
            "PUBLISH",
            "STOP",
            "RECOVERY",
            "DELETE",
            "MAINTENANCE_browse",
            "EXPERT_INTERVIEW_CONFIG",
            "EXPERT_INTERVIEW_COUNSELING",
            "PSYCHOLOGICAL_COUNSELING_CONFIG",
            "PSYCHOLOGICAL_CONSULTATION_TEACHER",
            "REPORT_BROWSE",
            "REPORT_EDIT",
            "USER_MANAGEMENT_BROWSE",
            "LOOK_REPORT",
            "LOOK_GRADE",
            "CANCEL_RESERVATION",
            "OK/PE_WESTERN_SURVEILLIANCE_SPEC",
            "OK/PE_DEFINE_DISEASE_TCM_SPEC",
            "OK/PE_DEFINE_INFECTION_SPEC",
            "OK/PE_RE_RESEARCH_TABLE",
            "OK/PE_DEFINE_ANESTHESIA",
            "OK/PE_DEFINE_ANATOMY",
            "OK/PE_PRE_SELECT_SURGERY",
            "OK/PE_CONFIRM_DISEASE_WESTERN_SPEC",
            "OK/PE_CONFIRM_DISEASE_TCM_SPEC",
            "OK/PE_CONFIRM_INFECTION_SPEC",
            "OK/PE_DEFINE_RE_RESEARCH_SPEC",
            "OK/PE_CONFIRM_ANESTHESIA",
            "OK/PE_CONFIRM_ANATOMY",
            "OK/PE_CONFIRM_SURGERY_SURVEILLIANCE_SPEC",
            "OK/PE_RECHECK_WESTERN_SURVEILLIANCE_SPEC",
            "OK/PE_RECHECK_CHINESE_SURVEILLIANCE_SPEC",
            "OK/PE_RECHECK_RE_RESEARCH_TABLE",
            "OK/PE_RECHECK_ANESTHESIA",
            "OK/PE_RECHECK_INFECTION_SPEC",
            "OK/PE_RECHECK_OPERATION_FACTOR",
            "OK/PE_RECHECK_ANATOMY");

    @Override
    public List<Permission> tree() {
        List<String> childList = Lists.newArrayList("QUESTION_BROWSE");
        List<Permission> allChildList = this.list(new QueryWrapper<Permission>().in("code", childList));
        List<Permission> list = this.list();
        Set<Permission> set = Sets.newHashSet();
        for (Permission per : allChildList) {
            Set<Permission> permissionUpList = getPermissionUpList(list, per);
            set.addAll(permissionUpList);
        }
        List<Permission> code = this.list(new QueryWrapper<Permission>().in("code", perList));
        set.addAll(code);

        List<Permission> menus = set.stream()
                //一级分类
                .filter(item -> item.getParentId() == 0)
                //找到当前菜单的子菜单
                .peek((menu) -> menu.setChildren(getChildrens(menu, set)))
                .sorted(Comparator.comparing(menu ->  menu.getSort()))
                .collect(Collectors.toList());
        return menus;
    }


    private List<Permission> getChildrens(Permission rootMenu, Set<Permission> allEntites) {
        List<Permission> children = allEntites.stream()
                //如果当前菜单的id等于它的上级i,说明属于上下级关系
                .filter(Permission -> Permission.getParentId()==(rootMenu.getId()))
                .peek((menu) -> menu.setChildren(getChildrens(menu, allEntites)))
                //递归查找，子菜单为空的话，排序会报空指针
                .sorted(Comparator.comparing(menu ->  menu.getSort()))
                .collect(Collectors.toList());
        return children;
    }




    /**
     * 向上查询所有上级
     * @param PermissionAll 所有集合
     * @param leafNodePermission 叶子节点
     */
    public static Set<Permission> getPermissionUpList(List<Permission> PermissionAll, Permission leafNodePermission){
        if(ObjectUtil.isNotEmpty(leafNodePermission)){
            Set<Permission> set = new HashSet<>();
            long parentId = leafNodePermission.getParentId();
            List<Permission> parentPermissions = PermissionAll.stream().filter(item -> item.getId()==(parentId)).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(parentPermissions)){
                Permission parentPermission = parentPermissions.get(0);
                set.add(parentPermission);
                Set<Permission> PermissionUpTree = getPermissionUpList(PermissionAll, parentPermission);
                if(PermissionUpTree!=null){
                    set.addAll(PermissionUpTree);
                }
                return set;
            }
        }
        return null;
    }

    @Override
    public List<String> querya() {
        List<Permission> tree = tree();
        List<String> collect = tree.stream()
                .map(Permission::getChildren)
                .flatMap(Collection::stream)
                .map(Permission::getCode)
                .collect(Collectors.toList());
        return collect;
    }
}
